﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//new
using System.IO;
using DataRecv.models.road;

namespace DataRecv
{
    public partial class editroad : Form
    {
        /// <summary>
        /// 路口编号
        /// </summary>
        public string roadid;
        
        /// <summary>
        /// 路口名称
        /// </summary>
        public string roadname;
        public string kkpath;
        public string djpath;
        public string kkvpath;
        public string djvpath;
        public string gbdm;
        public string xzqh;
        public string bz;
        /// <summary>
        /// 修改行的索引号
        /// </summary>
        public int rowindex;
        public string bmbh;

        public editroad()
        {
            InitializeComponent();
        }

        private void editroad_Load(object sender, EventArgs e)
        {
            try
            {
                textBox_id.Text = roadid;
                textBox_name.Text = roadname;
                textBox_gbdm.Text = gbdm;
                textBox_kkpath.Text = kkpath;
                textBox_djpath.Text = djpath;
                textBox_kkvpath.Text = kkvpath;
                textBox_djvpath.Text = djvpath;
                textBox_bmbh.Text = bmbh;
                textBox_xzqh.Text = xzqh;
                textBox_bz.Text = bz;
            }
            catch { }
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox_id.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，路口编号不能为空";
                    return;
                }
                if (textBox_name.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，路口名称不能为空";
                    return;
                }
                if (textBox_gbdm.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，国标代码不能为空";
                    return;
                }
                if (textBox_kkpath.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，卡口文件夹不能为空";
                    return;
                }
                if (textBox_djpath.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，电警文件夹不能为空";
                    return;
                }
                if (textBox_bmbh.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，部门编号不能为空";
                    return;
                }
                if (textBox_xzqh.Text == "")
                {
                    toolStripStatusLabel1.Text = "修改失败，行政区划不能为空";
                    return;
                }
                if (textBox_name.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "修改失败，路口名称不能含有_";
                    return;
                }
                if (textBox_kkpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "修改失败，卡口文件夹不能含有_";
                    return;
                }
                if (textBox_djpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "修改失败，电警文件夹不能含有_";
                    return;
                }
                if (textBox_kkvpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "修改失败，卡口虚拟文件夹不能含有_";
                    return;
                }
                if (textBox_djvpath.Text.Contains("_"))
                {
                    toolStripStatusLabel1.Text = "修改失败，电警虚拟文件夹不能含有_";
                    return;
                }
                if (!Directory.Exists(textBox_kkpath.Text)) Directory.CreateDirectory(textBox_kkpath.Text);
                if (!Directory.Exists(textBox_djpath.Text)) Directory.CreateDirectory(textBox_djpath.Text);


                //查找编号是否已经存在
                DataGridView dgv = ((road)(this.Owner)).dataGridView1;
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    DataGridViewRow r = dgv.Rows[i];
                    if ((textBox_id.Text == r.Cells["roadid"].Value.ToString()) && (i != rowindex))
                    {
                        toolStripStatusLabel1.Text = "修改失败,编号已存在";
                        return;
                    }
                }

                RoadParam param = new RoadParam();
                param.roadid = textBox_id.Text;
                param.gbdm = textBox_gbdm.Text;
                param.roadname = textBox_name.Text;
                param.kkpath = textBox_kkpath.Text;
                param.djpath = textBox_djpath.Text;
                param.kkvpath = textBox_kkvpath.Text;
                param.djvpath = textBox_djvpath.Text;
                param.bmbh = textBox_bmbh.Text;
                param.xzqh = textBox_xzqh.Text;
                param.bz = textBox_bz.Text;

                if (RoadParam.EditModel(param) == true)
                {
                    if (G.Roads != null)
                    {
                        Road r = (Road)G.Roads[param.roadid];
                        r.param = param;
                        G.Roads[param.roadid] = r;
                    }
                    dgv.Rows[rowindex].Cells["roadid"].Value = textBox_id.Text;
                    dgv.Rows[rowindex].Cells["roadname"].Value = textBox_name.Text;
                    dgv.Rows[rowindex].Cells["gbdm"].Value = textBox_gbdm.Text;
                    dgv.Rows[rowindex].Cells["kkpath"].Value = textBox_kkpath.Text;
                    dgv.Rows[rowindex].Cells["djpath"].Value = textBox_djpath.Text;
                    dgv.Rows[rowindex].Cells["kkvpath"].Value = textBox_kkvpath.Text;
                    dgv.Rows[rowindex].Cells["djvpath"].Value = textBox_djvpath.Text;
                    dgv.Rows[rowindex].Cells["bmbh"].Value = textBox_bmbh.Text;
                    dgv.Rows[rowindex].Cells["xzqh"].Value = textBox_xzqh.Text;
                    dgv.Rows[rowindex].Cells["bz"].Value = textBox_bz.Text;
                    ((Form_Main)this.Owner.Owner).UpDateTree();
                    toolStripStatusLabel1.Text = "修改成功";
                }
                else
                {
                    toolStripStatusLabel1.Text = "修改失败";
                }
            }
            catch (Exception er)
            {
                toolStripStatusLabel1.Text = er.Message;
            }
        }

        private void button_cancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        
        private void button_kkpath_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox_kkpath.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }

        private void button_djpath_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                if (folderBrowserDialog1.SelectedPath != "")
                {
                    textBox_djpath.Text = folderBrowserDialog1.SelectedPath;
                }
            }
        }
    }
}
